package com.example.dell.fastshopping.loginRegist;


import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dell.fastshopping.MainActivity;
import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.loginRegist.moldle.LoginRegistResetMoldle;
import com.example.dell.fastshopping.network.MyCallBack;
import com.example.dell.fastshopping.utils.DESUtils;
import com.example.dell.fastshopping.views.InputCode;
import com.example.dell.fastshopping.views.InputPassword;
import com.example.dell.fastshopping.views.MyEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by i7-1 on 2018/11/21.
 */

public class LoginActivity extends AppCompatActivity implements MyCallBack {

    @BindView(R.id.phone)
    MyEditText phone;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.input_password)
    InputPassword inputPassword;
    @BindView(R.id.input_code)
    InputCode inputCode;
    @BindView(R.id.show_mima)
    TextView showMima;
    @BindView(R.id.show_code)
    TextView showCode;
    @BindView(R.id.forgetPassword)
    TextView forgetPassword;
    @BindView(R.id.protocol_button)
    CheckBox protocolButton;
    @BindView(R.id.protocol)
    TextView protocol;
    private LoginRegistResetMoldle moldle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        moldle = ViewModelProviders.of(this).get(LoginRegistResetMoldle.class);
        setViewVisible(1);
        inputCode.setCallBack(this);
    }

    private void setViewVisible(int i) {
        inputPassword.setVisibility(i == 1 ? View.VISIBLE : View.GONE);
        inputCode.setVisibility(i == 1 ? View.GONE : View.VISIBLE);
        showMima.setVisibility(i == 1 ? View.GONE : View.VISIBLE);
        showCode.setVisibility(i == 1 ? View.VISIBLE : View.GONE);
    }


    @OnClick({R.id.forgetPassword, R.id.login, R.id.regist, R.id.show_mima, R.id.back, R.id.show_code})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.forgetPassword:
                intent = new Intent(LoginActivity.this, ForgetAndResetPassword.class);
                startActivity(intent);
                break;
            case R.id.login:
                login();
                break;
            case R.id.regist:
                intent = new Intent(LoginActivity.this, RegistActivity.class);
                startActivity(intent);
                break;
            case R.id.show_mima:
                inputCode.close();
                setViewVisible(1);
                break;
            case R.id.show_code:
                inputPassword.setText("");
                setViewVisible(2);
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    private void login() {
        moldle.login(phone.getText().toString().trim(), inputCode.getText(), inputPassword.getText().trim()).observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                if (aBoolean) {
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });

    }

    @Override
    public void callback() {
        moldle.getLoginCode(phone.getText().toString()).observe(LoginActivity.this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                if (aBoolean) {
                    inputCode.start();
                }
            }
        });
    }
}
